package com.epam.webproject.restaurant.jaxb.xml;

import com.epam.webproject.restaurant.jaxb.application.Application;
import com.epam.webproject.restaurant.jaxb.entities.Bill;
import com.epam.webproject.restaurant.jaxb.entities.Bills;
import com.epam.webproject.restaurant.jaxb.entities.ObjectFactory;
import org.apache.log4j.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Yelena_M
 * Date: 12.09.13
 * Time: 14:49
 * To change this template use File | Settings | File Templates.
 */
public class GeneratorXML {

    private final static Logger LOGGER = Logger.getLogger(Application.class);

    public static void generateBillXML(String file) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Bills.class);

            ObjectFactory objectFactory = new ObjectFactory();
            Bills bills = objectFactory.createBills();
            Bill bill = objectFactory.createBill();
            Bill bill2 = objectFactory.createBill();

            //Как использовать? Если 100 штук нужно
            bill.setAmount(12000);
            bill.setId("q12112");
            bill.setIdOrder("q12113");
            bill.setCookedDate(new Date());
            bill.setDeliveredDate(new Date());

            bill2.setAmount(1254000);
            bill2.setId("q121154");
            bill2.setIdOrder("q1211745");
            bill2.setCookedDate(new Date());
            bill2.setDeliveredDate(new Date());

            bills.restaurantBill.add(bill);
            bills.restaurantBill.add(bill2);

            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.marshal(bills, new FileOutputStream(file));
            //marshaller.marshal(bills, System.out);

        } catch (JAXBException e) {
            LOGGER.error(e);
        } catch (FileNotFoundException e) {
            LOGGER.error(e);
        }
    }
}
